﻿using Common;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace enterprise.Website.Core.ApiControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WebHomeController : ControllerBase
    {
        private readonly SqlContext _sqlContext;
        private readonly ICache _cache;
        public WebHomeController(SqlContext sqlContext, ICache cache)
        {
            _sqlContext = sqlContext;
            _cache = cache;
        }

        /// <summary>
        /// 获取首页配置详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{languageId}")]
        public async Task<ApiResult<WebHome>> Get(int languageId = 0)
        {
            var result = new ApiResult<WebHome> { Success = false, Message = "获取失败" };
            if (languageId > 0)
            {
                var WebHomeInfo = await _sqlContext.QueryInfoAsync<WebHome>(m => m.LanguageId == languageId);
                if (WebHomeInfo != null)
                {
                    result.Data = WebHomeInfo;
                    result.Success = true;
                    result.Message = "获取成功";
                }
            }
            return result;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        public async Task<ApiResult> Post([FromBody] WebHome data)
        {
            var result = new ApiResult { Success = false, Message = "保存失败" };
            if (data!=null)
            {
                var WebHomeInfo = await _sqlContext.QueryInfoAsync<WebHome>(m => m.LanguageId == data.LanguageId);
                if (WebHomeInfo != null)
                {
                    var Count = await _sqlContext.UpdateAsync<WebHome>(m => new WebHome
                    {
                        AboutusImg = data.AboutusImg,
                        AboutusText = data.AboutusText,
                        Background1 = data.Background1,
                        Background2 = data.Background2,
                        SeoDescritions=data.SeoDescritions,
                        SeoKeywords=data.SeoKeywords,
                    }, n => n.LanguageId == data.LanguageId);
                    if (Count > 0)
                    {
                        result.Success = true;
                        result.Message = "保存成功";
                        _cache.Remove($"{SystemConstConfig.WebHomeKey}_{data.LanguageId}");
                    }
                }
                else
                {
                    var Id = await _sqlContext.AddAsync<WebHome>(data);
                    if (Id > 0)
                    {
                        result.Success = true;
                        result.Message = "保存成功";
                        _cache.Remove($"{SystemConstConfig.WebHomeKey}_{data.LanguageId}");
                    }
                }
            }
            return result;
        }

    }
}
